<?php
// render blocks
$cts = $this->Bricabox_model->get_content_types($bricabox->id);
	
	if (count($blocks[$size]))
	{
		foreach ($blocks[$size] as $i => $block_data)
		{
			$method				 = "{$size}_{$block_data['data']->location}";
			$block_data['klass'] = BlockInstance::load_block_file(
				$block_data['template_path'],
				$block_data['parent']->name
			);
			$block_data['title'] = BlockInstance::title_for_block($block_data);
			
			echo "\t<!-- name: {$block_data['parent']->name} instance_id: {$block_data['data']->id} -->\n";
			echo "\t<div class=\"block_instance\" id=\"block_instance_{$block_data['data']->id}\">\n";
			if (!$edit_layout)
			{
				$has_prefs_var 	= "has_prefs_{$location}";
				$pref_link 		= ($this->Membership_model->is_owner($bricabox->id)
									&& $block_data['parent']->$has_prefs_var)
									? ' <a
										class="toggle toggle_prefs"
										href="#"
										onclick="return PrefPane.toggle('.$block_data['data']->id.');"
										>Edit</a>'
									: '';
				echo "\t\t<h3>{$block_data['title']}{$pref_link}</h3>\n";
				$block_data['klass']->$method($block_data, $content);
			}
			else
			{
				// draggables for editing the layout
				echo "\t\t<h3><span class=\"title\">{$block_data['title']}</span>&nbsp;<span class=\"dragme\">Drag this to reorder</span></h3>\n";
?>
<div class="edit_block_links">
	<a href="#" onclick="return editTitle(<?php echo $block_data['data']->id; ?>);">Edit this block's title</a>
	or <a href="#" onclick="return removeBlock(<?php echo $block_data['data']->id; ?>);"><img src="/assets/images/icons/cross.png" alt="X" />
		Remove from layout</a>
	<?php
	if ($location == 'content')
	{
		echo "<br />
<h3>Show this block for these kinds of entries:</h3>\n";
		foreach ($cts->result() as $ct)
		{
			$field_id = "exclude_{$block_data['data']->id}_{$ct->id}";
?>
<input onclick="setExclusion(<?php echo $block_data['data']->id; ?>, <?php echo $ct->id; ?>);" type="checkbox" id="<?php echo $field_id; ?>" name="<?php echo $field_id; ?>" value="1" <?php if (!$this->Bricabox_model->block_excluded($block_data['data']->id, $ct->id)) echo ' checked="checked"'; ?> />
	<label for="<?php echo $field_id; ?>"><?php echo ucfirst(Inflect::pluralize($ct->name)); ?></label><br />
<?php
		}
	}
	?>
</div>
<?php
			}
			echo "\n\t</div>\n\n";
		}
	}
	if ($edit_layout):
	?>
<script type="text/javascript" charset="utf-8">
$(document).ready(
	function ()
	{
		$('#blocks_<?php echo $size; ?>').Sortable({
			accept: 	'block_instance',
			helperclass: 'drophelper',
			tolerance: 	'intersect',
			handle: 	'.dragme',
			floats: 	true,
			onStop: 	function(ser)
			{
				var sorted_lg = $.SortSerialize('blocks_large');
				var sorted_sm = $.SortSerialize('blocks_small');
				var ids_lg	  = sorted_lg.hash.replace(/block_instance_/g, '').replace(/blocks_(large|small)\[\]\=/g, '').split('&').join('~');
				var ids_sm	  = sorted_sm.hash.replace(/block_instance_/g, '').replace(/blocks_(large|small)\[\]\=/g, '').split('&').join('~');
				$.post('/bricabox/<?php echo $bricabox->id; ?>/sort_blocks', {location: '<?php echo $location; ?>', size: 'large', ids: ids_lg});
				$.post('/bricabox/<?php echo $bricabox->id; ?>/sort_blocks', {location: '<?php echo $location; ?>', size: 'small', ids: ids_sm});
			}
		})
		$('#blocks_<?php echo $size; ?>_drop').Droppable({
			accept: 	'add_block',
			hoverclass: 'dropover',
			tolerance: 	'intersect',
			floats: 	true,
			onDrop: 	function(drag)
			{
				// steps to take:
				// 1. $.post the addition and get back some html
				// 2. append() the html to our sortable for this col
				// 3. call SortableAddItem on it
				// 4. done!
				
				var size_mode 	= this.id.match(/blocks_(large|small)_drop/);
				size_mode 		= size_mode[1];
				block_id		= drag.id.replace(/add_block_/, '');
				
				$.post('/bricabox/<?php echo $bricabox->id; ?>/add_block',
					{
						location: '<?php echo $location; ?>',
						size: size_mode,
						block_id: block_id
					}, function(data, textStatus)
				{
					var matched_id = data.match(/(block_instance_[\d]+)/);
					$('#blocks_'+size_mode).append(data).SortableAddItem(document.getElementById(matched_id[1]));
				});
			}
		});
	}
);
</script>
<?php
	endif;	
?>